/**
 * Created by sam on 16-11-21.
 */

var cheerio = require('cheerio');
var dao = require('./dao');
var utils = require('./utils');
var cookie = require('./cookie');
var common = require('../conf/common');

function loadComment(asin,ep,num,page) {
    //var s = new Date().getTime();
    var source = common.getSource();
    var url = common.getReviewUrl(asin,page);
    utils.getc(url,num,function (html) {
        if(html === 'timeout'){
            ep.emit("sleep", asin);
        }else{
            //console.log(html);
            var $ = cheerio.load(html);
            if($(".a-last").text().indexOf('robot')>0){
                console.log("Thread["+num+"] .... robot ....");
                //cookie.refresh();
                ep.emit("sleep", asin);
            }else{

                //Most recent 评论
                var yesterday = new Date();
                yesterday.setDate(yesterday.getDate()-2);
                var flag = true;
                var tmp = "";
                $("#cm_cr-review_list>div").each(function (index) {
                    var reviewId = $(this).attr("id");
                    if(reviewId) {
                        var comm_url = $("a.review-title",$(this)).attr('href');
                        var comm_grade = $("div:nth-child(1) > a:nth-child(1) > i > span",$(this)).text().replace(" out of 5 stars","");
                        var comm_title = $("a.review-title",$(this)).text();
                        var comm_name = $("a.author",$(this)).text();
                        var cust_url = $("a.author",$(this)).attr('href');
                        var comm_date = $("span.review-date",$(this)).text();
                        //#R1DQ6Y3OMB3JKV > div.a-row.a-spacing-mini.review-data.review-format-strip > a
                        var comm_color = $("div.a-row.a-spacing-mini.review-data.review-format-strip > a",$(this)).text();
                        var comment = $("span.review-text",$(this)).text().trim();
                        var comm_imgs = '';
                        $(".review-image-tile",$(this)).each(function () {
                            comm_imgs += $(this).attr("src")+";";
                        });

                        var comm_create = common.formatDate(comm_date);
                        var cd = comm_create.split("-");
                        //var comm_create = year+"-"+(month<10?'0'+month:month)+"-"+(day<10?'0'+day:day);
                        var cDay = new Date(cd[0],parseInt(cd[1])-1,parseInt(cd[2]));
                        flag = yesterday > cDay;
                        tmp = cDay;
                        /*console.log("================================");
                        console.log("reviewId:"+reviewId+",createDate:"+comm_create+",grade:"+comm_grade+",title:"+comm_title);
                        console.log("name:"+comm_name+",date:"+comm_date+",color:"+comm_color+",cust_url:"+cust_url);
                        console.log("comm_url:"+comm_url);
                        console.log("comm_cent:"+comment);
                        console.log("comm_imgs:"+comm_imgs);
                        console.log("================================");
                        console.log("");*/

                        var dd = {
                            page:page,
                            page_set:index,
                            source:source,
                            reviewId:reviewId,
                            comm_url:common.getDomain()+comm_url,
                            comment:utils.replaceEmoji(comment),
                            comm_imgs:comm_imgs,
                            comm_grade:comm_grade,
                            comm_title:utils.replaceEmoji(comm_title),
                            comm_name:comm_name,
                            cust_url:common.getDomain()+cust_url,
                            comm_date:comm_date,
                            comm_create:comm_create,
                            comm_color:comm_color.replace("Color: ",""),
                            asin:asin,
                        };
                        dao.saveProCMCR(dd);
                    }
                });
                //console.log(tmp);
                //console.log("flag:"+flag);
                var len =$("#cm_cr-review_list>div").length;
                if(flag || len < 10){//当前页10条评论，都是两天内的就翻页，否则就next asin
                    ep.emit('next_asin', asin);
                }else{
                    ep.emit('next_page', asin);
                }

                //var e = new Date().getTime();
                //console.log("Thread["+num+"] open time:"+(e-s))
            }
        }
    })
}

module.exports.loadComment = loadComment;

/*cookie.refresh(1 ,function () {
    loadComment('B01GIWFLQ6');
    //loadDetail('B00B7U6O2A');
});*/
//

//loadComment('B01GIWFLQ6');


